package com.baosight.sczx.zm.cl.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.zm.cl.domain.ZMCLReport_SafetyReport;
import com.baosight.sczx.zm.cl.domain.ZMCLReport_RiskRecord;

/**
 * 安全月报(报表展示)
 * <p>Title: ServiceZMCLR4.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-11-18 上午09:13:52  严洪浩  Create
 *</pre>
 */
public class ServiceZMCLR4 extends ServiceEPBase {
	
	/**
	 * 隐患月报查询所有的记录方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCL61</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-3下午01:56:44 高强
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo) {
		String reportDate = inInfo.getString("fdReportDate");
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdReportDate", reportDate);
		boolean validateValue = validate(inInfo,params);//有效性确认
		if(validateValue) {
			return inInfo;
		}
		String riskDate = reportDate.substring(0, 6);//将日期格式转变为"yyyy-mm"形式进行模糊查询
         if (riskDate.length() == 6)
         {
             riskDate = riskDate.replace("年","-0");
         }else
         {
             riskDate = riskDate.replace("年", "-");
         }
		StaticTools.setQueryParameters(inInfo, "fdRiskDate", riskDate, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdReportType", "1", true);//添加查询条件
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.offsetStr, 0);
		inInfo.getBlock(EiConstant.resultBlock).set(EiConstant.limitStr, 1000);
		inInfo.getBlock(EiConstant.resultBlock).set(EiConstant.isCountFlag, true);
		EiInfo outInfo = super.query(inInfo, "ZMCLReportRiskRecord.query", new ZMCLReport_RiskRecord(), false, null, EiConstant.queryBlock, EiConstant.resultBlock, EiConstant.resultBlock);
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cl.department", "orgCode", "orgName", true));//隐患单位
		return outInfo;
	}
	
	/**
	 * 检查是隐患月报是否显示
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-2  高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		List result = dao.query("ZMCLReportAssociation.query", params);
		if(result!=null) {
			
			if(result.size() > 0)
			{
				ZMCLReport_SafetyReport data = (ZMCLReport_SafetyReport)result.get(0);
				if(data.getFdStatus().equals("未发布"))
				{
					inInfo.setStatus(-1);
					StringBuffer msg = new StringBuffer();//组合提示语句
					msg.append("日期为" +data.getFdReportDate()+ "的月报尚未发布！");
					inInfo.setMsg(msg.toString());
					return true;
				}
			}else
			{
				inInfo.setStatus(-1);
				StringBuffer msg = new StringBuffer();//组合提示语句
				msg.append("当前月报不存在！");
				inInfo.setMsg(msg.toString());
				return true;
			}
		}
		return false;
	}
}
